//
//  CANavController.m
//  0.3camera
//
//  Created by 茶 on 2023/11/23.
//

#import "CANavController.h"

@interface CANavController () <UIGestureRecognizerDelegate>

@end

@implementation CANavController

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Nav_Back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];

    
}
- (void)back
{
    [self popViewControllerAnimated:YES];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.childViewControllers.count > 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
    self.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationBar.barTintColor = UIColor.whiteColor;
    self.navigationBar.tintColor = HexColor(@"#29282A");
    self.navigationBar.translucent = YES;
    NSDictionary *dic = @{NSForegroundColorAttributeName : HexColor(@"#29282A"), NSFontAttributeName : CAFontScaleRegular(19)};
    [self.navigationController.navigationBar setTitleTextAttributes:dic];

    UIImage *backImg = [UIImage imageWithColor:UIColor.clearColor size:CGSizeMake(1, 1)];
    if (@available(iOS 15.0, *)) {
        if (@available(iOS 15.0, *)) {
            UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
            [appearance configureWithOpaqueBackground];
            [appearance setBackIndicatorImage:backImg transitionMaskImage:backImg];
            [appearance setBackgroundColor:UIColor.clearColor];
            [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : HexColor(@"#29282A"), NSFontAttributeName : CAFontScaleRegular(19)}];
            [UINavigationBar appearance].standardAppearance = appearance;
            [UINavigationBar appearance].compactAppearance = appearance;
        } else {
            UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
            [appearance configureWithDefaultBackground];
            [appearance setBackIndicatorImage:backImg transitionMaskImage:backImg];
            [appearance setBackgroundColor:UIColor.clearColor];
            [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : HexColor(@"#29282A"), NSFontAttributeName : CAFontScaleRegular(19)}];
            [UINavigationBar appearance].standardAppearance = appearance;
            [UINavigationBar appearance].compactAppearance = appearance;
        }
    }
    
    // Do any additional setup after loading the view.
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleLightContent;
    }
}

//以 topViewController 的 preferredStatusBarStyle 来设置 statusBarStyle
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    if (self.viewControllers.count > 1) {
        self.topViewController.hidesBottomBarWhenPushed = NO;
    }
    
    NSArray<UIViewController *> *viewControllers = [super popToRootViewControllerAnimated:animated];
    // self.viewControllers has two items here on iOS14
    return viewControllers;
}
@end
